package ee.ttu.lecturerate;

import ee.ttu.lecturerate.data.User;
import ee.ttu.lecturerate.http.RequestAssembler;
import ee.ttu.lecturerate.http.RequestAsyncTask;
import ee.ttu.lecturerate.http.RequestInput;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class LoginActivity extends Activity
{
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
		Button loginButton = (Button)findViewById(R.id.loginButton);
		loginButton.setOnClickListener(new loginButtonOnClickListener());
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_login, menu);
		return true;
	}
	
	class loginButtonOnClickListener implements View.OnClickListener
	{
		@Override
		public void onClick(View v)
		{	
			EditText nameField = (EditText) findViewById(R.id.loginEditText);
			String name = nameField.getText().toString();
			
			
			if(!name.equals("")){
				/*
				 * With login
				RequestInput input = RequestAssembler.assembleGetUser(name);
				
				RequestAsyncTask task = new RequestAsyncTask(LoginActivity.this);
				task.execute(input);
				*/
				
				// without login
				User user = new User();
				user.setUsername(name);
				
				LectureRate application = (LectureRate) LoginActivity.this.getApplication();
				application.setActiveUser(user);
				
				RequestInput input = RequestAssembler.assembleGetLectureList();
				
				RequestAsyncTask task = new RequestAsyncTask(LoginActivity.this);
				task.execute(input);
				
			}
			else {
				TextView errorTextView = (TextView) findViewById(R.id.errorTextView);
				errorTextView.setText("Nimi sisestamata");
			}
			
			
		}
		
	}
}
